package com.liuxing.javaSpringBoot.modules.test.dao;

import com.liuxing.javaSpringBoot.modules.test.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

/**
 * Created by IntelliJ IDEA.
 * @File StudentRepository.java
 * @Auth liuxing
 * @Date 2021/8/20 13:28
 * @Email liuxing997@foxmail.com
 */
@Repository
public interface StudentRepository extends JpaRepository<Student,Integer>, JpaSpecificationExecutor<Student> {

    //根据姓名和邮箱查询学生信息
    Student findFirstByNameAndEmail(String name,String email);

    //根据id查询学生
    @Query(value = "from  Student where id = :id")
    Student getStudentById(@Param("id") int id);

    //原生根据id查询学生
    @Query(nativeQuery = true,value = "select * from  test_student where id = :id")
    Student getStudentByIdForSql(@Param("id") int id);

    //原生插入学生
    @Query(nativeQuery = true,value = "insert into test_student (name,email,age) " +
            "values (:#{#student.name},:#{#student.email},:#{#student.age})")
    @Modifying
    void insertStudent(@Param("student") Student student);

}
